package view;

import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JTable;
import javax.swing.border.TitledBorder;

import java.awt.SystemColor;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JPopupMenu;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import controller.SystemController;
import controller.AccountDataController;

import javax.swing.DefaultCellEditor;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.ScrollPaneConstants;
import javax.swing.ImageIcon;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ListSelectionModel;
import javax.swing.JCheckBox;
import javax.swing.UIManager;

import java.awt.Color;
import java.awt.CardLayout;

import model.Encryption;
import model.MyTableModel;

import org.jdesktop.swingx.JXSearchField;
import org.jdesktop.swingx.JXTextField;

import javax.swing.JPasswordField;

import java.awt.FlowLayout;
import java.util.Arrays;
import java.util.Vector;
import org.jdesktop.swingx.JXSearchField.LayoutStyle;

public class PanelSystem extends JPanel {
	private static final long serialVersionUID = -9046197820279907511L;
	private JTable table;
	private JPopupMenu popupMenu;
	private JXSearchField txtGiTrCn;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JPanel advanced;
	private JTextField textField_3;
	private JPasswordField n_oldPassword;
	private JPasswordField n_newPassword;
	private JPasswordField n_reenter;
	private JPanel account;
	private CardLayout cl_account;
	private static final String[] USER = { "User", "Admin" };
	private AccountDataController controlAcc;
	private SystemController controlSys;
	private JXTextField a_username;
	private JXTextField a_author;
	private JPasswordField a_password;
	private JPasswordField a_reenter;
	private JXTextField n_username;
	private JXTextField n_author;
	private JComboBox<String> n_combobox;
	private JComboBox<String> a_combobox;
	private JMenuItem mnDel;
	private JMenuItem mnNotify;
	private MyTableModel tableModel;
	private JPanel addPanel;
	private Vector<Vector<Object>> data;
	private JLabel a_label1;
	private JLabel a_label2;
	private JLabel a_label3;
	private JLabel a_label4;
	private JLabel a_label5;
	private JLabel n_label1;
	private JLabel n_label2;
	private JLabel n_label3;
	private JLabel n_label4;
	private JLabel n_label5;
	private JButton btnAdd;
	public static final Vector<String> COLUMN_NAMES = new Vector<String>(
			Arrays.asList("STT", "B\u1ECB kh\u00F3a",
					"T\u00EAn t\u00E0i kho\u1EA3n",
					"Ch\u1EE7 t\u00E0i kho\u1EA3n",
					"M\u1EADt kh\u1EA9u (M\u00E3 h\u00F3a)",
					"Quy\u1EC1n h\u1EA1n",
					"L\u1EA7n \u0111\u0103ng nh\u1EADp cu\u1ED1i"));
	private static final Icon DEAFAULT_ICON = new ImageIcon(
			ClassLoader.getSystemResource("./images/about.png"));
	private static final String DEFAULT_USERNAME = "Tên tài khoản tối thiểu 5 kí tự, tối đa 45 kí tự";
	private static final String DEFAULT_AUTHOR = "Tối thiểu 1 kí tự tối đa 45 kí tự";
	private static final String DEFAULT_PASS = "Ít nhất 6 kí tự hoặc số";

	public PanelSystem() {
		this.controlAcc = new AccountDataController(this);
		this.controlSys = new SystemController(this);
		setLayout(new BorderLayout(0, 7));
		createPopupMenu();

		JPanel findPanel = new JPanel();
		add(findPanel, BorderLayout.NORTH);
		findPanel.setLayout(new BorderLayout(0, 0));

		JPanel find = new JPanel();
		find.setBorder(new TitledBorder(null,
				"Th\u00F4ng tin t\u00ECm ki\u1EBFm", TitledBorder.LEADING,
				TitledBorder.TOP, null, SystemColor.textHighlight));
		findPanel.add(find, BorderLayout.CENTER);
		find.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC, },
				new RowSpec[] { FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblTmKim = new JLabel("Tìm kiếm");
		find.add(lblTmKim, "1, 1, right, default");

		txtGiTrCn = new JXSearchField();
		txtGiTrCn.setLayoutStyle(LayoutStyle.MAC);
		txtGiTrCn.setPrompt("Nhập giá trị cần tìm");
		find.add(txtGiTrCn, "3, 1, left, default");
		txtGiTrCn.setColumns(27);

		// TODO advanced find
		JLabel label = new JLabel("");
		label.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				super.mousePressed(e);
				controlSys.mousePressed(e);
			}
		});

		label.setToolTipText("Tìm kiếm nâng cao");
		label.setIcon(new ImageIcon(PanelSystem.class
				.getResource("/images/advan.png")));
		find.add(label, "5, 1, right, default");

		JButton button = new JButton("");
		button.setFocusable(false);
		button.setIcon(new ImageIcon(PanelSystem.class
				.getResource("/images/search.png")));
		find.add(button, "7, 1");

		advanced = new JPanel();
		advanced.setToolTipText("Dùng thiết đặt các tìm kiếm nâng cao hoặc lọc thông tin");
		advanced.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"),
				"T\u00ECm ki\u1EBFm n\u00E2ng cao", TitledBorder.LEADING,
				TitledBorder.TOP, null, new Color(51, 153, 255)));
		findPanel.add(advanced, BorderLayout.SOUTH);
		advanced.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.GROWING_BUTTON_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"), }, new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, }));

		JCheckBox chckbxTnTiKhon = new JCheckBox("Tên tài khoản");
		advanced.add(chckbxTnTiKhon, "1, 1");

		textField_2 = new JTextField();
		advanced.add(textField_2, "3, 1, fill, default");
		textField_2.setColumns(10);

		JCheckBox chckbxChTiKhon = new JCheckBox("Chủ tài khoản");
		advanced.add(chckbxChTiKhon, "5, 1");

		textField_1 = new JTextField();
		advanced.add(textField_1, "7, 1, fill, default");
		textField_1.setColumns(10);

		JCheckBox chckbxLnngNhp = new JCheckBox("Lần đăng nhập cuối");
		advanced.add(chckbxLnngNhp, "9, 1");

		textField = new JTextField();
		advanced.add(textField, "11, 1, fill, default");
		textField.setColumns(10);

		JCheckBox chckbxThucTnh = new JCheckBox("Trạng thái");
		advanced.add(chckbxThucTnh, "1, 3");

		JComboBox<String> comboBox_1 = new JComboBox<String>();
		comboBox_1.setModel(new DefaultComboBoxModel<String>(new String[] {
				"Bị khóa", "Không khóa" }));
		advanced.add(comboBox_1, "3, 3, fill, default");

		JCheckBox chckbxQuynHn = new JCheckBox("Quyền hạn");
		advanced.add(chckbxQuynHn, "5, 3");

		JComboBox<String> comboBox_2 = new JComboBox<String>();
		comboBox_2.setModel(new DefaultComboBoxModel<String>(new String[] {
				"User", "Admin" }));
		advanced.add(comboBox_2, "7, 3, fill, default");

		textField_3 = new JTextField();
		advanced.add(textField_3, "11, 3, fill, default");
		textField_3.setColumns(10);
		advanced.setVisible(false);

		JPanel centerList = new JPanel();
		centerList.setBorder(new TitledBorder(null,
				"Danh s\u00E1ch t\u00E0i kho\u1EA3n", TitledBorder.LEADING,
				TitledBorder.TOP, null, SystemColor.textHighlight));
		add(centerList, BorderLayout.CENTER);
		centerList.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		centerList.add(scrollPane, BorderLayout.CENTER);
		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

		// TODO table
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

		// table.setModel(new DefaultTableModel(new Object[][] { { "1",
		// Boolean.FALSE, "Admin", "Haric",
		// "24dggh20054d8hfb388861h71d6deg7", "Admin", "09/05/2014" }, },
		// new String[] { "STT", "B\u1ECB kh\u00F3a",
		// "T\u00EAn t\u00E0i kho\u1EA3n",
		// "Ch\u1EE7 t\u00E0i kho\u1EA3n",
		// "M\u1EADt kh\u1EA9u (M\u00E3 h\u00F3a)",
		// "Quy\u1EC1n h\u1EA1n",
		// "L\u1EA7n \u0111\u0103ng nh\u1EADp cu\u1ED1i" }) {
		// private static final long serialVersionUID = -1954553144772680356L;
		// @SuppressWarnings("rawtypes")
		// Class[] columnTypes = new Class[] { Object.class, Boolean.class,
		// Object.class, Object.class, Object.class, Object.class,
		// Object.class };
		//
		// @SuppressWarnings({ "rawtypes", "unchecked" })
		// public Class getColumnClass(int columnIndex) {
		// return columnTypes[columnIndex];
		// }
		// });

		data = new Vector<Vector<Object>>();
		controlAcc.loadData(data);
		table.setFillsViewportHeight(true);
		table.setAutoCreateRowSorter(true);
		table.getTableHeader().setReorderingAllowed(false);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				super.mouseReleased(e);
				controlSys.mouseReleased(e);
			}
		});
		// test
		JCheckBox check = new JCheckBox();
		DefaultCellEditor dc = new DefaultCellEditor(check);
		table.getColumnModel().getColumn(2).setCellEditor(dc);
		// end test
		scrollPane.setViewportView(table);

		JPanel south = new JPanel();
		add(south, BorderLayout.SOUTH);
		south.setLayout(new BorderLayout(0, 0));

		// TODO account
		cl_account = new CardLayout(0, 0);
		account = new JPanel();
		account.setBorder(new TitledBorder(null,
				"Chi ti\u1EBFt t\u00E0i kho\u1EA3n", TitledBorder.LEADING,
				TitledBorder.TOP, null, SystemColor.textHighlight));
		south.add(account, BorderLayout.NORTH);
		account.setLayout(cl_account);
		account.setVisible(false);

		// TODO add panel
		addPanel = new JPanel();
		account.add(addPanel, "add");
		addPanel.setLayout(new FormLayout(
				new ColumnSpec[] { FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] {
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblTnTiKhon = new JLabel();
		lblTnTiKhon.setText("Tên tài khoản");
		addPanel.add(lblTnTiKhon, "1, 1, left, default");

		a_username = new JXTextField();
		a_username.setName("user");
		a_username.addFocusListener(controlAcc);
		a_username.setActionCommand("user");
		a_username.addActionListener(controlAcc);
		a_username.setPrompt("Tên dùng đăng nhập");
		addPanel.add(a_username, "3, 1, fill, default");

		a_label1 = new JLabel("");
		a_label1.setIcon(new ImageIcon(PanelSystem.class
				.getResource("/images/about.png")));
		addPanel.add(a_label1, "5, 1");

		JLabel lblChTiKhon = new JLabel();
		lblChTiKhon.setText("Chủ tài khoản");
		addPanel.add(lblChTiKhon, "1, 3, left, default");

		a_author = new JXTextField();
		a_author.setName("author");
		a_author.addFocusListener(controlAcc);
		a_author.setActionCommand("author");
		a_author.addActionListener(controlAcc);
		a_author.setPrompt("Chủ sở hữu tài khoản");
		addPanel.add(a_author, "3, 3, fill, default");

		a_label2 = new JLabel("");
		a_label2.setIcon(new ImageIcon(PanelSystem.class
				.getResource("/images/about.png")));
		addPanel.add(a_label2, "5, 3");

		JLabel lblQuynHn = new JLabel();
		lblQuynHn.setText("Quyền hạn");
		addPanel.add(lblQuynHn, "1, 5, left, default");

		a_combobox = new JComboBox<String>();
		a_combobox.setModel(new DefaultComboBoxModel<String>(USER));
		addPanel.add(a_combobox, "3, 5, fill, default");

		a_label3 = new JLabel("");
		a_label3.setIcon(new ImageIcon(PanelSystem.class
				.getResource("/images/about.png")));
		addPanel.add(a_label3, "5, 5");

		JLabel lblMtKhu = new JLabel();
		lblMtKhu.setText("Mật khẩu");
		addPanel.add(lblMtKhu, "1, 7, left, default");

		a_password = new JPasswordField();
		a_password
				.setToolTipText("Mật khẩu nên bao gồm chữ thường, chữ hoa, số và các kí tự đặc biệt");
		a_password.setName("pass");
		a_password.addFocusListener(controlAcc);
		a_password.setActionCommand("pass");
		a_password.addActionListener(controlAcc);
		addPanel.add(a_password, "3, 7, fill, default");

		a_label4 = new JLabel("");
		a_label4.setToolTipText(DEFAULT_PASS);
		a_label4.setIcon(new ImageIcon(PanelSystem.class
				.getResource("/images/about.png")));
		addPanel.add(a_label4, "5, 7");

		JLabel lblNhpLiMt = new JLabel();
		lblNhpLiMt.setText("Nhập lại MK");
		addPanel.add(lblNhpLiMt, "1, 9, left, default");

		a_reenter = new JPasswordField();
		a_reenter
				.setToolTipText("Nhập lại mật khẩu bên trên để xác nhận độ chính xác");
		a_reenter.setName("repass");
		a_reenter.addFocusListener(controlAcc);
		a_reenter.setActionCommand("repass");
		a_reenter.addActionListener(controlAcc);
		addPanel.add(a_reenter, "3, 9, fill, default");

		a_label5 = new JLabel("");
		a_label5.setIcon(new ImageIcon(PanelSystem.class
				.getResource("/images/about.png")));
		addPanel.add(a_label5, "5, 9");

		// TODO notify panel
		JPanel notifyPanel = new JPanel();
		account.add(notifyPanel, "notify");
		notifyPanel.setLayout(new FormLayout(
				new ColumnSpec[] { FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] {
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblTnTiKhon_1 = new JLabel("Tên tài khoản");
		notifyPanel.add(lblTnTiKhon_1, "1, 1, left, default");

		n_username = new JXTextField();
		n_username.setPrompt("Tên dùng đăng nhập");
		notifyPanel.add(n_username, "3, 1, 5, 1, fill, default");

		n_label1 = new JLabel("");
		n_label1.setIcon(new ImageIcon(PanelSystem.class
				.getResource("/images/about.png")));
		notifyPanel.add(n_label1, "9, 1");

		JLabel lblChTiKhon_1 = new JLabel("Chủ tài khoản");
		notifyPanel.add(lblChTiKhon_1, "1, 3, left, default");

		n_author = new JXTextField();
		n_author.setPrompt("Chủ sở hữu tài khoản");
		notifyPanel.add(n_author, "3, 3, fill, default");

		JLabel lblQuynHn_1 = new JLabel("Quyền hạn");
		notifyPanel.add(lblQuynHn_1, "5, 3, left, default");

		n_combobox = new JComboBox<String>();
		n_combobox.setModel(new DefaultComboBoxModel<String>(USER));
		notifyPanel.add(n_combobox, "7, 3, fill, default");

		n_label2 = new JLabel("");
		n_label2.setIcon(new ImageIcon(PanelSystem.class
				.getResource("/images/about.png")));
		notifyPanel.add(n_label2, "9, 3");

		JLabel lblMtKhuC = new JLabel("Mật khẩu cũ");
		notifyPanel.add(lblMtKhuC, "1, 5, left, default");

		n_oldPassword = new JPasswordField();
		notifyPanel.add(n_oldPassword, "3, 5, 5, 1, fill, default");

		n_label3 = new JLabel("");
		n_label3.setIcon(new ImageIcon(PanelSystem.class
				.getResource("/images/about.png")));
		notifyPanel.add(n_label3, "9, 5");

		JLabel lblMtKhuMi = new JLabel("Mật khẩu mới");
		notifyPanel.add(lblMtKhuMi, "1, 7, left, default");

		n_newPassword = new JPasswordField();
		notifyPanel.add(n_newPassword, "3, 7, 5, 1, fill, default");

		n_label4 = new JLabel("");
		n_label4.setIcon(new ImageIcon(PanelSystem.class
				.getResource("/images/about.png")));
		notifyPanel.add(n_label4, "9, 7");

		JLabel lblNhLiMk = new JLabel("Nhập lại MK");
		notifyPanel.add(lblNhLiMk, "1, 9, left, default");

		n_reenter = new JPasswordField();
		notifyPanel.add(n_reenter, "3, 9, 5, 1, fill, default");

		n_label5 = new JLabel("");
		n_label5.setIcon(new ImageIcon(PanelSystem.class
				.getResource("/images/about.png")));
		notifyPanel.add(n_label5, "9, 9");

		JPanel n_func = new JPanel();
		FlowLayout fl_n_func = (FlowLayout) n_func.getLayout();
		fl_n_func.setVgap(0);
		notifyPanel.add(n_func, "3, 11, 5, 1, fill, fill");

		JButton btnThm = new JButton("Lưu");
		n_func.add(btnThm);

		JButton btnThot = new JButton("Thoát");
		btnThot.setActionCommand("exit");
		btnThot.addActionListener(controlAcc);
		n_func.add(btnThot);

		JPanel a_func = new JPanel();
		FlowLayout fl_a_func = (FlowLayout) a_func.getLayout();
		fl_a_func.setVgap(0);
		addPanel.add(a_func, "3, 11, fill, fill");

		btnAdd = new JButton("Thêm");
		btnAdd.setActionCommand("add");
		btnAdd.addActionListener(controlAcc);
		a_func.add(btnAdd);

		JButton button_2 = new JButton("Thoát");
		button_2.setActionCommand("exit");
		button_2.addActionListener(controlAcc);
		a_func.add(button_2);

		JPanel func = new JPanel();
		south.add(func, BorderLayout.SOUTH);
		func.setLayout(new BorderLayout(0, 0));

		JLabel lblTngSTi = new JLabel("Tống số tài khoản: ");
		func.add(lblTngSTi, BorderLayout.WEST);

		JLabel lblnhngTiKhon = new JLabel(
				" (những tài khoản chưa lưu sẽ không được tính)");
		func.add(lblnhngTiKhon, BorderLayout.CENTER);

		JPanel panelButton = new JPanel();
		func.add(panelButton, BorderLayout.EAST);

		JButton btnThmTiKhon_1 = new JButton("Thêm tài khoản");
		btnThmTiKhon_1.setActionCommand("show");
		btnThmTiKhon_1.addActionListener(controlSys);
		panelButton.add(btnThmTiKhon_1);

		JButton btnThmTiKhon = new JButton("Lưu thay đổi");
		btnThmTiKhon.setEnabled(false);
		btnThmTiKhon.setActionCommand("save");
		btnThmTiKhon.addActionListener(controlSys);
		btnThmTiKhon.setPreferredSize(btnThmTiKhon_1.getPreferredSize());
		panelButton.add(btnThmTiKhon);
	}

	// TODO create popup menu
	private void createPopupMenu() {
		popupMenu = new JPopupMenu();

		JMenuItem mntmThmTiKhon = new JMenuItem("Thêm");
		mntmThmTiKhon.setActionCommand("add");
		mntmThmTiKhon.addActionListener(controlSys);
		popupMenu.add(mntmThmTiKhon);

		popupMenu.addSeparator();

		mnDel = new JMenuItem("Xóa");
		mnDel.setActionCommand("del");
		mnDel.addActionListener(controlAcc);
		popupMenu.add(mnDel);

		mnNotify = new JMenuItem("Sửa");
		mnNotify.setActionCommand("notify");
		mnNotify.addActionListener(controlSys);
		popupMenu.add(mnNotify);
	}

	// TODO control
	// used by add account button
	public void showAddPanel() {
		showPanel("add");
		account.setVisible(!account.isVisible());
	}

	// used by notify menu
	public void showNotifyPanel() {
		showPanel("notify");
		account.setVisible(true);
	}

	// used by add menu
	public void setVisibleAddPanel() {
		showPanel("add");
		account.setVisible(true);
	}

	// TODO exit() used by cancel button
	public void exit() {
		clearAddPanel();
		clearNotifyPanel();
		account.setVisible(false);
	}

	public void clearAddPanel() {
		// set text
		a_username.setText("");
		a_author.setText("");
		a_combobox.setSelectedIndex(0);
		a_password.setText("");
		a_reenter.setText("");
		// set default icon, tool tip text
		a_label1.setIcon(DEAFAULT_ICON);
		a_label1.setToolTipText(DEFAULT_USERNAME);
		a_label2.setIcon(DEAFAULT_ICON);
		a_label2.setToolTipText(DEFAULT_AUTHOR);
		a_label3.setIcon(DEAFAULT_ICON);
		a_label4.setIcon(DEAFAULT_ICON);
		a_label5.setIcon(DEAFAULT_ICON);
		a_label5.setToolTipText(DEFAULT_PASS);
	}

	public void clearNotifyPanel() {
		// set text
		n_username.setText("");
		n_author.setText("");
		n_combobox.setSelectedIndex(0);
		n_oldPassword.setText("");
		n_newPassword.setText("");
		n_reenter.setText("");
		// set default icon
		n_label1.setIcon(DEAFAULT_ICON);
		n_label2.setIcon(DEAFAULT_ICON);
		n_label3.setIcon(DEAFAULT_ICON);
		n_label4.setIcon(DEAFAULT_ICON);
		n_label5.setIcon(DEAFAULT_ICON);
	}

	private void showPanel(String name) {
		cl_account.show(account, name);
	}

	public void showAdvancedFind() {
		advanced.setVisible(!advanced.isVisible());
	}

	public void enableMenu(boolean b) {
		mnDel.setEnabled(b);
		mnNotify.setEnabled(b);
	}

	public void showPopupMenu(MouseEvent e) {
		if (e.isPopupTrigger()) {
			if (table.getSelectedRowCount() != 0)
				enableMenu(true);
			else
				enableMenu(false);
			popupMenu.show(e.getComponent(), e.getX(), e.getY());
		}
	}

	/*
	 * TODO check user name too long
	 */
	public boolean checkUserName() {
		String tmp = a_username.getText().trim();
		if (tmp.length() == 0) {
			changeIcon(a_label1, -1);
			a_label1.setToolTipText(DEFAULT_USERNAME);
		} else if (tmp.length() < 5 || tmp.length() > 45) {
			changeIcon(a_label1, 0);
			a_label1.setToolTipText(DEFAULT_USERNAME);
		} else {
			for (int i = 0; i < data.size(); i++) {
				if (tmp.equalsIgnoreCase((String) data.get(i).get(2))) {
					changeIcon(a_label1, 0);
					a_label1.setToolTipText("Tên tài khoản đã được sử dụng");
					return false;
				}
			}
			changeIcon(a_label1, 1);
			a_label1.setToolTipText(DEFAULT_USERNAME);
			a_author.requestFocus();
			return true;
		}
		return false;
	}

	// TODO check author
	public void checkAuthor() {
		String tmp = a_author.getText().trim();
		if (tmp.equals("")) {
			changeIcon(a_label2, -1);
			a_label2.setToolTipText(DEFAULT_AUTHOR);
			return;
		}
		for (int i = 0; i < data.size(); i++) {
			if (tmp.equalsIgnoreCase((String) data.get(i).get(3))) {
				changeIcon(a_label2, 2);
				a_label2.setToolTipText("Chủ tài khoản này đã có tài khoản khác trên hệ thống");
				return;
			}
		}
		changeIcon(a_label2, 1);
		a_password.requestFocus();
		a_label2.setToolTipText(DEFAULT_AUTHOR);
	}

	/**
	 * TODO check pass warning max length of password
	 */
	public boolean checkPass() {
		char[] tmp = a_password.getPassword();
		if (tmp.length == 0) {
			changeIcon(a_label4, -1);
		} else if (tmp.length < 6) {
			changeIcon(a_label4, 0);
		} else {
			changeIcon(a_label4, 1);
			a_reenter.requestFocus();
			checkReenter();
			return true;
		}
		return false;
	}

	public boolean checkReenter() {
		char[] tmp = a_reenter.getPassword();
		if (tmp.length == 0) {
			changeIcon(a_label5, -1);
			a_label5.setToolTipText(DEFAULT_PASS);
		} else if (Arrays.equals(tmp, a_password.getPassword())) {
			changeIcon(a_label5, 1);
			return true;
		} else {
			changeIcon(a_label5, 0);
			a_label5.setToolTipText("Mật khẩu không trùng khớp");
		}
		return false;
	}

	public void changeIcon(JLabel label, int type) {
		String name = null;
		switch (type) {
		case 0: // invalid
			name = "delete";
			break;
		case 1: // valid
			name = "valid";
			break;
		case 2: // waring
			name = "warning";
			break;
		default:
			label.setIcon(DEAFAULT_ICON);
			return;
		}
		StringBuilder str = new StringBuilder("/images/").append(name).append(
				".png");
		Icon icon = new ImageIcon(getClass().getResource(str.toString()));
		label.setIcon(icon);
	}

	public String[] getQueryAdd() {
		String[] query = new String[5];
		query[0] = "n";
		query[1] = a_username.getText();
		query[2] = a_author.getText();
		String tmp = new StringBuilder(a_username.getText().toLowerCase())
				.append(a_password.getPassword()).toString();
		query[3] = Encryption.encrypt(tmp);
		query[4] = a_combobox.getItemAt(a_combobox.getSelectedIndex());
		return query;
	}

	public String getQueryDel() {
		return (String) data.get(table.getSelectedRow()).get(2);
	}

	// TODO see
	public void setTableModel(Vector<Vector<Object>> data) {
		if (tableModel == null)
			tableModel = new MyTableModel(data, COLUMN_NAMES);
		tableModel.setDataVector(data, COLUMN_NAMES);
		table.setModel(tableModel);
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(0).setPreferredWidth(45);
		table.getColumnModel().getColumn(0).setMaxWidth(45);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(1).setMaxWidth(75);
	}

	public Vector<Vector<Object>> getData() {
		return this.data;
	}

	public JXTextField getA_username() {
		return a_username;
	}

	public JButton getBtnAdd() {
		return btnAdd;
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(new PanelSystem());
		frame.pack();
		frame.setVisible(true);
	}

}